package com.neu.view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.neu.dao.impl.PatientDao;
import com.neu.pojo.Patient;
import com.neu.pojo.Worker;
import com.neu.service.impl.PatientService;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

public class AddPatient extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private PatientService patientService = PatientService.getInstance();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AddPatient frame = new AddPatient(new Worker());
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AddPatient(Worker w) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 471, 446);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("姓名");
		lblNewLabel.setBounds(77, 22, 72, 26);
		contentPane.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("生日");
		lblNewLabel_1.setBounds(77, 58, 72, 26);
		contentPane.add(lblNewLabel_1);
		
		JLabel lblNewLabel_2 = new JLabel("性别");
		lblNewLabel_2.setBounds(77, 100, 72, 26);
		contentPane.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("身份证号");
		lblNewLabel_3.setBounds(77, 140, 72, 26);
		contentPane.add(lblNewLabel_3);
		
		JLabel lblNewLabel_4 = new JLabel("手机号");
		lblNewLabel_4.setBounds(77, 176, 72, 26);
		contentPane.add(lblNewLabel_4);
		
		JLabel lblNewLabel_5 = new JLabel("紧急联系人");
		lblNewLabel_5.setBounds(77, 218, 72, 26);
		contentPane.add(lblNewLabel_5);
		
		JLabel lblNewLabel_6 = new JLabel("紧急联系人电话");
		lblNewLabel_6.setBounds(77, 262, 119, 26);
		contentPane.add(lblNewLabel_6);
		
		textField = new JTextField();
		textField.setBounds(206, 25, 171, 34);
		contentPane.add(textField);
		textField.setColumns(10);
		
		textField_1 = new JTextField();
		textField_1.setColumns(10);
		textField_1.setBounds(206, 61, 171, 34);
		contentPane.add(textField_1);
		
		textField_2 = new JTextField();
		textField_2.setColumns(10);
		textField_2.setBounds(206, 103, 171, 34);
		contentPane.add(textField_2);
		
		textField_3 = new JTextField();
		textField_3.setColumns(10);
		textField_3.setBounds(206, 143, 171, 34);
		contentPane.add(textField_3);
		
		textField_4 = new JTextField();
		textField_4.setColumns(10);
		textField_4.setBounds(206, 179, 171, 34);
		contentPane.add(textField_4);
		
		textField_5 = new JTextField();
		textField_5.setColumns(10);
		textField_5.setBounds(206, 221, 171, 34);
		contentPane.add(textField_5);
		
		textField_6 = new JTextField();
		textField_6.setColumns(10);
		textField_6.setBounds(206, 265, 171, 34);
		contentPane.add(textField_6);
		
		JButton btnNewButton = new JButton("确定");
		btnNewButton.setBounds(77, 348, 97, 23);
		contentPane.add(btnNewButton);
		btnNewButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if(textField.getText().length() != 0&&textField_1.getText().length() != 0&&textField_2.getText().length() != 0
						&&textField_3.getText().length() != 0&&textField_4.getText().length() != 0&&
						textField_5.getText().length() != 0&&textField_6.getText().length() != 0) {
					//用户输入正确了， 注册
					if(PatientService.getInstance().add(new Patient(textField.getText(), textField_1.getText(), textField_2.getText(), textField_3.getText(), textField_4.getText(), textField_5.getText(), textField_6.getText()))) {
						JOptionPane.showMessageDialog(null, "添加病人成功");//提示
						WorkerUI workerUI = new WorkerUI(w);//跳转到新窗口
						workerUI.setVisible(true);
						setVisible(false);
					}else {
						JOptionPane.showMessageDialog(null, "添加病人失败,病人已存在");//提示
					}
				}else {
					JOptionPane.showMessageDialog(null, "添加病人失败,请输入信息");//提示
				}
				
			}
		});
		
		JButton btnNewButton_1 = new JButton("返回");
		btnNewButton_1.setBounds(228, 348, 97, 23);
		contentPane.add(btnNewButton_1);
		btnNewButton_1.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {//跳转到新窗口
				WorkerUI workerUI = new WorkerUI(w);
				workerUI.setVisible(true);
				setVisible(false);
			}
		});
		
		//窗口居中
		setLocationRelativeTo(null);
	}

}
